package de.tud.binpacking.heuristic.construction;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import de.tud.binpacking.data.Batch;
import de.tud.binpacking.data.Order;
import de.tud.binpacking.instance.Instance;

public class NearestNeighborBS extends AStartHeuristic {

	public NearestNeighborBS(Instance instance) {
		super(instance);
	}

	@Override
	public String toString() {
		return "Nearest Neighbor - biggest to smallest order, and try to add a order to a not full cart";		
	}
	
	@Override
	protected boolean findSolution() {
		
		List<Batch> batches = new ArrayList<Batch>();
		
		//Order by size
		List<Order> orders = this.instance.getOrders();
		Collections.sort(orders);
		
		//add Orders to Batches
		loop: for(Order order : orders) {
			for(Batch batch : batches) {
				if(batch.add(order)) {
					continue loop;
				}
			}
			batches.add(new Batch(this.instance, order));
		}
		for(Batch batch : batches){
			solution.addBatch(batch);
		}
		return true;
	}

}
